﻿using MediatR;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC_Uthorization.Read.API.Application.Command.RoleCommand;
using RBAC_Uthorization.Read.API.Application.Command.UserRoleCommand;
using RBAC_Uthorization.Read.API.DTO;
using RBAC_Uthorization.Read.API.DTO.Request;
using RBAC_Uthorization.Read.API.DTO.Response.offlc;
using RBAC_Uthorization.Read.API.DTO.Response.Role;
using System.IdentityModel.Tokens.Jwt;


namespace RBAC_Uthorization.Read.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class RoleController : ControllerBase
    {
        private readonly IMediator _mediator;
        

        public RoleController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 角色显示
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PagingDto>> show(int pageIndex=1,int pageSize=10)
        {
          var entity=  await _mediator.Send(new RoleShowCommand(){
              pageIndex=pageIndex,
              pageSize=pageSize,
              offlc_id = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value,
              sys_id = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Jti)?.Value
          });

            return entity;
        }
        /// <summary>
        /// 角色select显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<RoleSelectDto>>> RoleSelectShow()
        {
            var entity = await _mediator.Send(new RoleSelectCommand() { 
                offlc_id = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value,
                sys_id = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Jti)?.Value
            });
            return entity;
        }
        /// <summary>
        /// 根据id查询角色详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<RoleFindDto>> RoleFind(int id)
        {
            var entity = await _mediator.Send(new RoleFindCommand() { id = id, 
                offlc_id = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value, 
                sys_id = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Jti)?.Value });
            return entity;
        }
        /// <summary>
        /// 根据用户id查询角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<int>>> RoleFindByUserId(int id)
        {
            var entity = await _mediator.Send(new UserRoleUserIdCommand() {
                id = id,
                offlc_id = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value,
                sys_id = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Jti)?.Value });
            return entity;
        }
        

    }
}
